import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
const store = new Vuex.Store({
    state: {
		token: uni.getStorageSync('token') || "",
		admin:  uni.getStorageSync('admin') || "",
		userinfo: "",
		addressList: [],
		collegeList: [],
		gradeList: [],
		specialityList: [],
		sellinginfo: "",
    },
    mutations: {
		SET_TOKEN(state,val){
			state.token = val;
			uni.setStorageSync('token', val)
		},
		DEL_TOKEN(state){
			state.token = ''
			uni.removeStorageSync('token')
		},
		SET_ADMIN(state,val){
			state.admin = val;
			uni.setStorageSync('admin', val)
		},
		DEL_ADMIN(state){
			state.admin = ''
			uni.removeStorageSync('admin')
		},
		SET_INFO(state,val){
			state.userinfo = val
		},
		DEL_INFO(state){
			state.userinfo = ''
		},
		SET_ADDRESS(state,val){
			state.addressList = val
		},
		SET_COLLEGE(state,val){
			state.collegeList = val
		},
		SET_GRADE(state,val){
			state.gradeList = val
		},
		SET_SPECIALITY(state,val){
			state.specialityList = val
		},
		SET_SELLING(state,val){
			state.sellinginfo = val
		},
    },
	actions: {
		 // 获取个人信息
		getUserInfo({commit}) {
			return Vue.prototype.$httpApi.personal().then(res => {
				commit('SET_INFO', res.data)
			})
		},
		// 获取地址
		getAddress({commit}) {
			return Vue.prototype.$httpApi.addressArea().then(res => {
				var str = []
				var arr = res.data.province
				arr.map(item => {
				    let obj = {
						id: item.id,
						name: item.name,
				        area: item.city,
				    }
				    str.push(obj)
				});
				commit('SET_ADDRESS', str)
			})
		},
		// 获取学院
		getCollege({commit}) {
			return Vue.prototype.$httpApi.college().then(res => {
				commit('SET_COLLEGE', res.data.college)
			})
		},
		// 获取年级
		getGrade({commit}) {
			return Vue.prototype.$httpApi.grade().then(res => {
				commit('SET_GRADE', res.data)
			})
		},
		// 获取专业
		getSpeciality({commit}) {
			return Vue.prototype.$httpApi.speciality().then(res => {
				commit('SET_SPECIALITY', res.data)
			})
		},
		// 卖书首页 卖书须知、流程、地址
		getSelling({commit}) {
			return Vue.prototype.$httpApi.sellingInfo().then(res => {
				commit('SET_SELLING', res.data)
			})
		},
	},
	modules: {
		
	},
});
export default store;